<?php

namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\PhpRedis;

class CommentController extends BaseController
{

    private $redis;

    public function __construct()
    {
        parent::__construct();

        $this->redis = new PhpRedis(0);

    }

    public function article()
    {
        $this->display();
    }

    public function articleComment()
    {
        $params = $this->getParams();
        $page = I('get.p', 1);
        $pageSize = 10;
        $data = D('Comment', 'Logic')->getArticleCommentList('article_comment', $params, $page, $pageSize);
        $Page = new AjaxPage($data['total'], $pageSize, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);
        $this->assign('comment', $data['list']);
        $this->display();
    }

    public function articleCommentStatus()
    {
        $data = [
            'comment_id'  => I('post.comment_id'),
            'status'      => I('post.status'),
            'update_time' => time()
        ];

        //评论禁用发送消息
        if ($data['status'] == 3) {
            $commentInfo = D('Comment', 'Logic')->getArticleComment($data['comment_id']);
            $commentContent = mb_substr($commentInfo['content'], 0, 10, 'utf-8');
            $keyName  = getRedisKeyByCode('REDIS_KEY', '100000');
            $this->redis->lpush($keyName, json_encode(
                    [
                        'bizContent' =>
                            [
                                'userId'     => (string)$commentInfo['user_id'],
                                'msgTitle'   => '我的评论违规禁用',
                                'msgContent' => '【我的评论】您的评论“' . $commentContent . '”审核不通过。',
                                'msgParam'   => '',
                            ],
                        'bizType'    => 11
                    ]
                )
            );
        }

        $res = D('Comment', 'Logic')->saveArticleComment($data);

        if ($res) {
            $this->helpJsonResult(200);
        } else {
            $this->helpJsonResult(300);
        }
    }

    public function expertArticle()
    {
        $this->display();
    }

    public function expertArticleComment()
    {
        $params = $this->getParams();
        $page = I('get.p', 1);
        $pageSize = 10;
        $data = D('Comment', 'Logic')->getArticleCommentList('expert_article_comment', $params, $page, $pageSize);
        $Page = new AjaxPage($data['total'], $pageSize, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        $show = $Page->show();// 分页显示输出

        $this->assign('page', $show);
        $this->assign('comment', $data['list']);
        $this->display();
    }

    public function expertArticleCommentStatus()
    {
        $data = [
            'comment_id'  => I('post.comment_id'),
            'status'      => I('post.status'),
            'update_time' => time()
        ];
        //评论禁用发送消息
        if ($data['status'] == 3) {
            $expertCommentInfo = D('Comment', 'Logic')->getExpertArticleComment($data['comment_id']);
            $expertCommentContent = mb_substr($expertCommentInfo['content'], 0, 10, 'utf-8');
            $keyName  = getRedisKeyByCode('REDIS_KEY', '100000');
            $this->redis->lpush($keyName, json_encode(
                    [
                        'bizContent' =>
                            [
                                'userId'     => (string)$expertCommentInfo['user_id'],
                                'msgTitle'   => '我的评论违规禁用',
                                'msgContent' => '【我的评论】您的评论“' . $expertCommentContent . '”审核不通过。',
                                'msgParam'   => '',
                            ],
                        'bizType'    => 11
                    ]
                )
            );
        }

        $res = D('Comment', 'Logic')->saveExpertArticleComment($data);

        if ($res) {
            $this->helpJsonResult(200);
        } else {
            $this->helpJsonResult(300);
        }
    }

    private function getParams()
    {
        $params = [
            'comment_id' => I('get.comment_id'),
            'content'    => I('get.content'),
            'user_id'    => I('get.user_id'),
            'mobile'     => I('get.mobile'),
            'article_id' => I('get.article_id'),
            'status'     => I('get.status'),
            'report'     => I('get.report'),
            'start_time' => I('get.start_time'),
            'end_time'   => I('get.end_time')
        ];
        return $params;
    }
}
